import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	CreateDateColumn,
	ManyToOne,
	JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { Order } from "../../order/entities/order.entity";
import { DateTransformer } from "../../../common/transformers/date.transformer";

@Entity("points_records")
export class PointsRecord {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({
		type: "enum",
		enum: ["收入", "支出"],
		default: "收入",
	})
	type: string;

	@Column()
	amount: number;

	@Column()
	balance: number;

	@Column({ length: 255 })
	description: string;

	@Column({ nullable: true })
	orderId: number;

	@CreateDateColumn({
		name: "created_at",
		type: "datetime",
		transformer: new DateTransformer(),
		nullable: false,
	})
	createdAt: Date;

	// 关系
	@ManyToOne(() => User, (user) => user.pointsRecords)
	@JoinColumn({ name: "user_id" })
	user: User;

	@Column()
	userId: number;

	@ManyToOne(() => Order, { nullable: true })
	@JoinColumn({ name: "order_id" })
	order: Order;
}
